// ################
// ### Varibles ###
float4x4 World;
float4x4 View;
float4x4 Projection; 

float3 Eye;
float4 SpecularColor;

float4 ElementColor;
float TimeFactor;

float4 AmbientColor;
float AmbientLevel;

float3 DiffuseDirection;
float4 DiffuseColour;
float DiffuseIntensity;

Texture Texture;
sampler TextureSampler = sampler_state
{
	texture = <Texture>;
	magfilter = LINEAR;
	minfilter = LINEAR;
	mipfilter = LINEAR;
	AddressU = mirror;
	AddressV = mirror;
};

// #######################
// ### Data Structures ###
struct VertexData
{
	float4 Position : POSITION;
	float3 Normal : TEXCOORD1;
	float3 Position3D : TEXCOORD2;
	float3 View : TEXCOORD3;
};

struct PixelData {
	float4 Color : COLOR0;
};

// ########################
// ### Shader Functions ###
VertexData ActorVertexDeformation(float4 inPos : POSITION0, float3 inNormal: NORMAL0)
{
	VertexData output = (VertexData)0;

	float4x4 vp = mul(View, Projection);
	float4x4 wvp = mul(World, vp);
	float4 position = mul(inPos, wvp).xyzw;	
	
	output.Position = position;
	output.Normal = normalize(mul(inNormal, (float3x3)World));
	output.Position3D = mul(inPos, World);
	output.View = normalize(float4(Eye, 1.0) - mul(inPos, World));
	return output;
}
PixelData ActorSpecularElementShader(VertexData inData)
{
	PixelData output = (PixelData)0;

	float4 norm = float4(inData.Normal, 1.0f);
	float4 diffuse = saturate(dot(-DiffuseDirection, norm));	

	float4 reflect = normalize(2 * diffuse * norm - float4(DiffuseDirection, 1.0));
	float4 specular = pow(saturate(dot(reflect, inData.View)), 15);


	float3 light = (AmbientColor * AmbientLevel) + (DiffuseIntensity * DiffuseColour * diffuse) + (specular * SpecularColor);
	output.Color = float4(ElementColor * light, 1.0f); 
	return output;
} 


// ####################
// #### Techniques ####
technique ActorSpecular
{
	pass Pass0
	{
		VertexShader = compile vs_2_0 ActorVertexDeformation();
		PixelShader = compile ps_2_0 ActorSpecularElementShader();
	}
}